//
//  IMListTableViewCell.m
//  TQY
//
//  Created by 甘世清 on 2024/10/18.
//

#import "IMListTableViewCell.h"
#import "IMContentManager.h"
@implementation IMListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)SQ_Style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self.contentView.backgroundColor = UIColor.clearColor;
    UIView * bgView = [[UIView alloc]init];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(4, 12, 4, 12));
    }];
    [bgView addSubview:self.headerView];
    [bgView addSubview:self.badgeBg];
    [bgView addSubview:self.nameLabel];
    [bgView addSubview:self.messageLabel];
    [bgView addSubview:self.timeLabel];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(60);
            make.centerY.equalTo(bgView);
            make.left.equalTo(bgView).offset(12);
    }];
    [self.badgeBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(50);
        make.right.equalTo(self.headerView.mas_right).offset(-6);
        make.top.equalTo(self.headerView);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView).offset(7);
        make.left.equalTo(self.headerView.mas_right).offset(12);
        make.height.mas_equalTo(22);
    }];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.headerView.mas_bottom).offset(-7);;
        make.left.equalTo(self.headerView.mas_right).offset(12);
        make.right.equalTo(bgView.mas_right).offset(-80);
        make.height.mas_equalTo(24);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel);
        make.right.equalTo(bgView.mas_right).offset(-12);
    }];
    
}
- (void)setModel:(IMListModel *)model{
    _model = model;
    [self.headerView.headerImage sd_setImageWithURL:SQURLImage(model.toUserPortrait) placeholderImage:SQGetImage(@"1")];
    if (model.messageCount > 0) {
        self.badge.hidden = NO;
        self.badge.badgeText = [NSString stringWithFormat:@"%ld",(long)model.messageCount];
    } else {
        self.badge.hidden = YES;
    }
    self.nameLabel.text = model.toUserName;
    _messageLabel.text = model.contentString;
 
    self.timeLabel.text = [IMContentManager SQ_getTimeString:model.sendTime];
}
-(void)SQ_headerViewButtonClick{
    PeopleDetailViewController * vc = [PeopleDetailViewController alloc];
    vc.uid = self.model.toUser;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = FONT_SIZE(16);
        _nameLabel.textColor = SQColor(@"#8760FF");
        _nameLabel.backgroundColor = UIColor.clearColor;
     }
    return _nameLabel;
}
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font = FONT_REGULAR(12);
        _timeLabel.textColor = SQColor(@"#828282");
        _timeLabel.backgroundColor = UIColor.clearColor;
     }
    return _timeLabel;
}
 
- (UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = FONT_REGULAR(12);
        _messageLabel.textColor = SQColor666666;
        _messageLabel.backgroundColor = UIColor.clearColor;
     }
    return _messageLabel;
}
- (HeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HeaderView alloc]init];
        _headerView.headerImage.contentMode = UIViewContentModeScaleAspectFill;
        _headerView.clipsToBounds = YES;
        _headerView.layer.cornerRadius = 8;
        _headerView.layer.masksToBounds = YES;
        [_headerView.btn addTarget:self action:@selector(SQ_headerViewButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerView;
}
- (JSBadgeView *)badge{
    if (!_badge) {
        _badge = [[JSBadgeView alloc]initWithParentView:self.badgeBg alignment:JSBadgeViewAlignmentTopRight];
        _badge .backgroundColor = SQColor(@"#FF3B30");
        _badge.badgePositionAdjustment = CGPointMake(0, 0);
        _badge.badgeTextFont = FONT_REGULAR(12);
        _badge.layer.cornerRadius = 8;
        _badge.layer.masksToBounds = YES;
    }
    return _badge;
}
- (UIView *)badgeBg{
    if (!_badgeBg) {
        _badgeBg = [[UIView alloc]init];
        _badgeBg.backgroundColor = UIColor.clearColor;
        _badgeBg.userInteractionEnabled = NO;
    }
    return _badgeBg;
}
@end
